<?php
namespace app\admin\controller;
use app\model\Image as ImageModel;
use think\Db;

use app\lib\baidu\Api as baiduApi;


class Image extends Base
{
    public function __construct(){
        $model = new ImageModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '分类';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function assignFields(){
        $model = $this->model;

        $fields = [
            'image'=>['name'=>'<i class="mdi mdi-camera" style="font-size:20px;"></i> <br />图片上传 ','type'=>'file'],
            'tags'=>['name'=>'标签（以逗号分隔，最多2个）','type'=>'text'],
            'url'=>['name'=>'跳转链接','type'=>'text'],
        ];
        $page_info = $this->page_info;
        if ( in_array($page_info['action'], ['edit']) ) {
            unset($fields['image']);
        }
        $valids = createPageValid($fields);
        $this->assign([
            'fields'=>$fields,
            'valids'=>$valids
        ]);

    }

    public function assignSearchs(){
        $model = $this->model;
        $search_params = [
            [
                ['key'=>'name','name'=>'名称','type'=>'text'],
                ['key'=>'tags','name'=>'标签','type'=>'text'],
                ['key'=>'status','name'=>'状态','type'=>'select1_1','params'=>$model->getStatusList()],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'name','type'=>'like'],
            ['key'=>'status','type'=>'='],
            ['key'=>'tags','type'=>'like'],
        ];
        $map = getSearchParam($map_params);

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        // 显示筛选
        $this->assignSearchs();

        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();
        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

    public function add(){
        $model = $this->model;
        if (request()->isPost()) {
            $fields = [
                'must'=>['image'],
                'nomust'=>['description','brief','tags','url']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
            $params = [];
            if (!empty($save_data['brief'])) {
                foreach ($save_data['brief']['name'] as $k => $vo) {
                    $params[$save_data['brief']['name'][$k]] = $save_data['brief']['value'][$k];
                }
            }
            $save_data['brief'] = json_encode($params,JSON_UNESCAPED_UNICODE);

            $image = explode('/',$save_data['image']);
            $save_data['name'] = $image[count($image) - 1];
            $image = explode('.',$save_data['name']);
            // $save_data['name'] = $image[0];
            $save_data['extension'] = $image[count($image) - 1];
            $save_data['ctime'] = time();

            $params['url'] = $save_data['image'];
            $result = (new baiduApi())->imageUpload($save_data['image'],json_encode($params,JSON_UNESCAPED_UNICODE),$params['tags']);
            if ($result['status'] == 0) {
                $save_data['status'] = -1;
                $save_data['remark'] = $result['msg'];
            }else{
                $save_data['status'] = 1;
                $save_data['cont_sign'] = $result['cont_sign'];
            }

            if ( $model->save($save_data) ) {
                $this->success("添加成功",url('index'));
            }else{
                $this->error("添加失败");
            }
        }

        $this->assignFields();
        return view('save', [
            
        ]);
    }

    public function edit(){
        $id = input('id');
        if (empty($id)) { $this->error('id不能为空'); }
        
        $model = $this->model;
        if (request()->isPost()) {
            $fields = [
                'nomust'=>['description','tags','url']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
            // $params = [];
            // if (!empty($save_data['brief'])) {
            //     foreach ($save_data['brief']['name'] as $k => $vo) {
            //         $params[$save_data['brief']['name'][$k]] = $save_data['brief']['value'][$k];
            //     }
            // }
            // $save_data['brief'] = json_encode($params,JSON_UNESCAPED_UNICODE);
        }

        return parent::updateData($id,$save_data);
    }




   
}
